package uk.ac.ebi.bioinvindex.model.impl;


import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.Collection;

/**
 * User: Nataliya Sklyar (nsklyar@ebi.ac.uk)
 * Date: Jul 11, 2007
 */

@MappedSuperclass
public abstract class AnnotatableImpl extends IdentifiableImpl {

	protected String acc;

	protected Collection<Annotation> annotations = new ArrayList<Annotation>();

	protected AnnotatableImpl() {
		super();
	}

	@Column(name = "ACC", unique = true)
	public String getAcc() {
		return acc;
	}

	public void setAcc(String acc) {
		this.acc = acc;
	}

	//ToDo: add mapping to subclasses
	@Transient
	public Collection<Annotation> getAnnotations() {
		return annotations;
	}

	protected void setAnnotations(Collection<Annotation> annotations) {
		this.annotations = annotations;
	}

	public void addAnnotation(Annotation annotation) {
		if (annotation == null) {
			throw new IllegalArgumentException("Null value for Annotation is not allowed.");
		}
		getAnnotations().add(annotation);
	}

	public boolean removeAnnotation(Annotation annotation) {
		return annotations.remove(annotation);
	}

	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		AnnotatableImpl that = (AnnotatableImpl) o;

		if (acc != null ? !acc.equals(that.acc) : that.acc != null) return false;
		if (annotations != null ? !annotations.equals(that.annotations) : that.annotations != null) return false;

		return true;
	}

	public int hashCode() {
		int result;
		result = (acc != null ? acc.hashCode() : 0);
		result = 31 * result + (annotations != null ? annotations.hashCode() : 0);
		return result;
	}
}
